//
//  ReadDetailViewController.m
//  product_B
//
//  Created by lanou on 16/5/29.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ReadDetailViewController.h"
#import "ReadTableViewController.h"
#import "ReadMainModel.h"
#import "ReadserialModel.h"
#import "ReadQuestionModel.h"
#import "ReadEverListViewController.h"
#import "MusicView.h"
@interface ReadDetailViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)NSMutableArray *indexArray;

@property (nonatomic,strong)MusicPlayerManager *manager;
@property (nonatomic,strong)MusicView *musicView;
@property (nonatomic,strong)NSTimer *publicTime;
@property (nonatomic,assign)BOOL isShow;
@end

@implementation ReadDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.indexArray = [NSMutableArray array];
    NSString *string;
    if (self.indexY == 0) {
        string = @"短篇";
        
    }else if (_indexY == 1){
        string = @"连载";
    }else{
        string = @"问答";
    }
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = string;
    [self setScrollView];
    [self.scrollView setContentOffset:CGPointMake(_indexX * kWidth, 0)];
    self.rightBarbutton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"音乐 旋转.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarbuttonAction:)];
    self.navigationItem.rightBarButtonItem = _rightBarbutton;
    self.manager = [MusicPlayerManager shareInstance];

    self.musicView = [[MusicView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 150)];
    
    self.musicView.backgroundColor = [UIColor whiteColor];
    
    self.musicView.layer.shadowOffset = CGSizeMake(1, 1);
    self.musicView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.musicView.layer.shadowOpacity = 5;
    self.musicView.layer.shadowRadius = 1;
    self.musicView.layer.cornerRadius = 10;
    self.musicView.title.frame = CGRectMake(10, 70, 250, 60);
    self.musicView.title.numberOfLines = 0;
    [self.view addSubview:self.musicView];
    self.musicView.hidden = YES;
    
    self.publicTime = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(publicTimeAction) userInfo:nil repeats:YES];
}
- (void)publicTimeAction{
    if ([MusicPlayerManager shareInstance].player.currentItem.duration.timescale == 0 || [MusicPlayerManager shareInstance].player.currentTime.timescale == 0) {
        return;
    }
    long long int currentTime = [MusicPlayerManager shareInstance].player.currentTime.value / [MusicPlayerManager shareInstance].player.currentTime.timescale;
    long long int allTime = [MusicPlayerManager shareInstance].player.currentItem.duration.value / [MusicPlayerManager shareInstance].player.currentItem.duration.timescale;
    
    if (currentTime == allTime) {
        [[MusicPlayerManager shareInstance]replaceItemWithUrlString:[MusicPlayerManager shareInstance].playUrl];
    }
    
    
}
- (void)rightBarbuttonAction:(UIBarButtonItem *)button{
    [_musicView setMusicViewWithModel:[MusicPlayerManager shareInstance].musicModel];
    if (!_isShow) {
        self.musicView.hidden = NO;
        [self.view bringSubviewToFront:self.musicView];
        [self.musicView.button addTarget:self action:@selector(musicViewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        if ([MusicPlayerManager shareInstance].isPlay) {
            [self.musicView.button setBackgroundImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
        }else{
            [self.musicView.button setBackgroundImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
        }
    }else{
        self.musicView.hidden = YES;
    }
    _isShow = !_isShow;
}

- (void)musicViewButtonAction:(UIButton *)sender{
    if ([MusicPlayerManager shareInstance].isPlay) {
        [sender setBackgroundImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
    }else{
        [sender setBackgroundImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
    }
    
    [_manager playerPlayAndPause];
    
}
- (void)viewWillAppear:(BOOL)animated{
    if (!_manager.isPlay) {
        self.navigationItem.rightBarButtonItem = nil;
    }else{
       
        self.navigationItem.rightBarButtonItem = self.rightBarbutton;
        
    }
}
- (void)setScrollView{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight )];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.alwaysBounceHorizontal = YES;
    self.scrollView.bounces = YES;
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(kWidth * 10, 0);
    
    [self.view addSubview:_scrollView];
    
        ReadTableViewController *tableVC = [[ReadTableViewController alloc]init];
        
        if (self.indexY == 0) {
            ReadMainModel *model = self.modelArray[_indexX];
            tableVC.modelID = model.content_id;
        }else if (_indexY == 1){
            ReadserialModel *model = self.modelArray[_indexX];
            tableVC.modelID = model.myID;
        }else{
            ReadQuestionModel *model = self.modelArray[_indexX];
            tableVC.modelID = model.question_id;
        }
        tableVC.indexY = _indexY;
        tableVC.view.frame = CGRectMake(kWidth*_indexX, 0, kWidth, kHeight );
        
        [_scrollView addSubview:tableVC.view];
        [self addChildViewController:tableVC];
    [self.indexArray addObject:[NSString stringWithFormat:@"%ld",_indexX]];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = self.scrollView.contentOffset.x / kWidth;
    for (NSString *string in self.indexArray) {
        if (string.intValue == index) {
            return;
        }
    }
    ReadTableViewController *tableVC = [[ReadTableViewController alloc]init];
    
    if (self.indexY == 0) {
        ReadMainModel *model = self.modelArray[index];
        
        tableVC.modelID = model.content_id;
    }else if (_indexY == 1){
        ReadserialModel *model = self.modelArray[index];
        tableVC.modelID = model.myID;
    }else{
        ReadQuestionModel *model = self.modelArray[index];
        tableVC.modelID = model.question_id;
    }
    tableVC.indexY = _indexY;
    tableVC.view.frame = CGRectMake(kWidth*index, 0, kWidth, kHeight );
    
    [_scrollView addSubview:tableVC.view];
    [self addChildViewController:tableVC];
    [self.indexArray addObject:[NSString stringWithFormat:@"%ld",index]];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.scrollView.contentOffset.x > self.scrollView.contentSize.width - kWidth) {
        ReadEverListViewController *everVC = [[ReadEverListViewController alloc]init];
        [self.navigationController pushViewController:everVC animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
